package br.usp.pcs.acs.negocios.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.usp.pcs.acs.negocios.dao.interfaces.AeronaveDaoInterface;
import br.usp.pcs.acs.negocios.dto.AeronaveDto;


public class AeronaveDao implements AeronaveDaoInterface {
	@Override
	public void edit(AeronaveDto inDto)
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_U);
			
			pstmt.setInt(5, inDto.getId());//id
			pstmt.setInt(1,inDto.getVOO_Portao_id());
			pstmt.setInt(2,inDto.getVOO_id());
			pstmt.setString(3,inDto.getModelo());
			pstmt.setInt(4,inDto.getCapacidade());
			
			pstmt.executeUpdate();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}
	@Override
	public void remove(int id) 
	{
		Connection conn = null;
		PreparedStatement pstmt = null;		
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(CRUD_D);			
			pstmt.setInt(1, id);			
			pstmt.execute();
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}
	@Override
	public AeronaveDto findById(int id) {
		String busca =  "SELECT * FROM aeronave WHERE id = " + id;
		ArrayList<AeronaveDto> ret= finding(busca);
		return ret.isEmpty()?null:ret.get(0);
	}
	public ArrayList<AeronaveDto>  findByModelo(String mods) {
		String busca =  "SELECT * FROM aeronave WHERE modelo = \"" + mods+ "\"";
		return finding(busca);
	}
	@Override
	public ArrayList<AeronaveDto> findAll() {		
		return finding(CRUD_R);
	}
	
	
	
	@Override
	public void insert(AeronaveDto inDto)
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_C);
			
			pstmt.setNull(1, java.sql.Types.INTEGER);//id
			pstmt.setInt(2,inDto.getVOO_Portao_id());
			pstmt.setInt(3,inDto.getVOO_id());
			pstmt.setString(4,inDto.getModelo());
			pstmt.setInt(5,inDto.getCapacidade());
			
			pstmt.execute();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}

	private ArrayList<AeronaveDto> finding(String busca) {
		ArrayList<AeronaveDto> LoutDto = new ArrayList<AeronaveDto>();
		AeronaveDto tmpDto = null;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(busca);			
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				tmpDto = new AeronaveDto();
				tmpDto.setId(rs.getInt(1));
				tmpDto.setVOO_Portao_id(rs.getInt(2));
				tmpDto.setVOO_id(rs.getInt(3));
				tmpDto.setModelo(rs.getString(4));
				tmpDto.setCapacidade(rs.getInt(5));
				
				LoutDto.add(tmpDto);
			}
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
		
		return LoutDto;
	}
}
